//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "HaganPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Quote.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/CmsCouponPricer.h>
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (boost::shared_ptr<QuantLib::HaganPricer>& childNative, Object^ owner) : CCmsCouponPricer(CHaganPricer::typeid)
{
#ifdef HANDLE
	_phHaganPricer = NULL;
#endif
	_ppHaganPricer = &childNative;
    _ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
}
Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (QuantLib::HaganPricer& childNative, Object^ owner) : CCmsCouponPricer(CHaganPricer::typeid)
{
#ifdef HANDLE
	_phHaganPricer = NULL;
#endif
	_ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (&childNative);
    _ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
    _HaganPricerOwner = owner;
    _CmsCouponPricerOwner = owner;
}

Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (CHaganPricer^ copy) : CCmsCouponPricer(CHaganPricer::typeid)
{
#ifdef HANDLE
	_phHaganPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (copy->GetShared());
        _ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
    }
}
Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (System::Type^ t) : CCmsCouponPricer(CHaganPricer::typeid)
{
#ifdef HANDLE
	_phHaganPricer = NULL;
#endif
	if (!t->IsSubclassOf(CHaganPricer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (QuantLib::Handle<QuantLib::HaganPricer>& childNative, Object^ owner)  : CCmsCouponPricer(CHaganPricer::typeid)
{
	_phHaganPricer = &childNative;
	_ppHaganPricer = &static_cast<boost::shared_ptr<QuantLib::HaganPricer>>(childNative.currentLink());
    _ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
    _HaganPricerOwner = owner;
}
Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (QuantLib::Handle<QuantLib::HaganPricer> childNative)  : CCmsCouponPricer(CHaganPricer::typeid)
{
	_phHaganPricer = &childNative;
	_ppHaganPricer = &static_cast<boost::shared_ptr<QuantLib::HaganPricer>>(childNative.currentLink());
    _ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CHaganPricer::CHaganPricer (QuantLib::HaganPricer childNative)  : CCmsCouponPricer(CHaganPricer::typeid)
{
#ifdef HANDLE
	_phHaganPricer = NULL;
#endif
	_ppHaganPricer = new boost::shared_ptr<QuantLib::HaganPricer> (new QuantLib::HaganPricer (childNative));
    _ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (boost::dynamic_pointer_cast<QuantLib::CmsCouponPricer> (*_ppHaganPricer));
}
#endif

Cephei::QL::Cashflows::CHaganPricer::~CHaganPricer ()
{
    if (_ppHaganPricer != NULL)
    {
	    delete _ppHaganPricer;
        _ppHaganPricer = NULL;
    }
}
Cephei::QL::Cashflows::CHaganPricer::!CHaganPricer ()
{
    if (_ppHaganPricer != NULL)
    {
	    delete _ppHaganPricer;
    }
}
QuantLib::HaganPricer& Cephei::QL::Cashflows::CHaganPricer::GetReference ()
{
    if (_ppHaganPricer == NULL) throw gcnew NativeNullException ();
	return **_ppHaganPricer;
}
boost::shared_ptr<QuantLib::HaganPricer>& Cephei::QL::Cashflows::CHaganPricer::GetShared ()
{
    if (_ppHaganPricer == NULL) throw gcnew NativeNullException ();
	return *_ppHaganPricer;
}
QuantLib::HaganPricer* Cephei::QL::Cashflows::CHaganPricer::GetPointer ()
{
    if (_ppHaganPricer == NULL) throw gcnew NativeNullException ();
	return &**_ppHaganPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HaganPricer>& Cephei::QL::Cashflows::CHaganPricer::GetHandle ()
{
	if (_phHaganPricer == NULL)
	{
		_phHaganPricer = new Handle<QuantLib::HaganPricer> (*_ppHaganPricer);
	}
	return *_phHaganPricer;
}
#endif
bool Cephei::QL::Cashflows::CHaganPricer::HasNative () 
{
	return (_ppHaganPricer != NULL);
}

Double Cephei::QL::Cashflows::CHaganPricer::CapletPrice (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHaganPricer)->capletPrice ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CHaganPricer::CapletRate (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppHaganPricer)->capletRate ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CHaganPricer::FloorletPrice (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHaganPricer)->floorletPrice ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CHaganPricer::FloorletRate (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppHaganPricer)->floorletRate ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CHaganPricer::MeanReversion::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHaganPricer)->meanReversion ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IHaganPricer^ Cephei::QL::Cashflows::CHaganPricer::SetMeanReversion (Cephei::QL::IQuote^ meanReversion)
{
    CQuote^ _CmeanReversion;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CmeanReversion = safe_cast<CQuote^> (meanReversion);
        _CmeanReversion->Lock();
        Handle<QuantLib::Quote>& _meanReversion = static_cast<Handle<QuantLib::Quote>&> (_CmeanReversion->GetHandle ()); 
    	(*_ppHaganPricer)->setMeanReversion ( _meanReversion );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CmeanReversion != nullptr) _CmeanReversion->Unlock();
    }
}
Double Cephei::QL::Cashflows::CHaganPricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHaganPricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CHaganPricer::SwapletRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppHaganPricer)->swapletRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CHaganPricerVector::CHaganPricerVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Cashflows::CHaganPricerVector::~CHaganPricerVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Cashflows::CHaganPricerVector::!CHaganPricerVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Cashflows::CHaganPricerVector::CHaganPricerVector (std::vector<QuantLib::HaganPricer>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Cashflows::CHaganPricerVector::CHaganPricerVector (std::vector<QuantLib::HaganPricer> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::HaganPricer> (vec);
}
#endif
Cephei::QL::Cashflows::CHaganPricerVector::CHaganPricerVector (std::vector<boost::shared_ptr<QuantLib::HaganPricer>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Cashflows::CHaganPricerVector::CHaganPricerVector (std::vector<QuantLib::Handle<QuantLib::HaganPricer>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Cashflows::CHaganPricerVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Cashflows::IHaganPricer^ Cephei::QL::Cashflows::CHaganPricerVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw gcnew RangeException ();
		QuantLib::HaganPricer& native = (*_pVValue)[index];
		return gcnew CHaganPricer (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw gcnew RangeException ();
		QuantLib::HaganPricer& native = *(*_pVBoost)[index];
		return gcnew CHaganPricer (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw gcnew RangeException ();
		QuantLib::HaganPricer& native = *((*_pVHand)[index].currentLink ());
		return gcnew CHaganPricer (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Cashflows::CHaganPricerVector::default::set (int index, Cephei::QL::Cashflows::IHaganPricer^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CHaganPricer^>(value)->GetReference ());
		}
        else
        {
            QuantLib::HaganPricer& c = (*_pVValue)[index];
            c = dynamic_cast<CHaganPricer^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CHaganPricer^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::HaganPricer>& c = (*_pVBoost)[index];
		c = dynamic_cast<CHaganPricer^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::HaganPricer> ());
		}
		QuantLib::Handle<QuantLib::HaganPricer>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::HaganPricer>& b = static_cast<boost::shared_ptr<QuantLib::HaganPricer>>(c.currentLink());
		b = dynamic_cast<CHaganPricer^>(value)->GetShared ();
	}
#endif
}
Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^ Cephei::QL::Cashflows::CHaganPricerVector::CItem::get (int index) 
{
	return CellBuilder::Instance->Create<IHaganPricer^> (dynamic_cast<IHaganPricer^>(default[index]));
}

void Cephei::QL::Cashflows::CHaganPricerVector::CItem::set (int index, Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^ value) 
{
	default[index] = safe_cast<IHaganPricer^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::HaganPricer>& Cephei::QL::Cashflows::CHaganPricerVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::HaganPricer>>& Cephei::QL::Cashflows::CHaganPricerVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::HaganPricer>>& Cephei::QL::Cashflows::CHaganPricerVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Cashflows::CHaganPricerVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::HaganPricer> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::HaganPricer>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::HaganPricer>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of HaganPricer that can be passed into other function </summary> */
IVector<Cephei::QL::Cashflows::IHaganPricer^>^ Cephei::QL::Cashflows::CHaganPricer_Factory::CreateVector ()
 {
	 return gcnew CoVector<IHaganPricer^> (gcnew CHaganPricerVector ());
 }
IVector<Cephei::QL::Cashflows::IHaganPricer^>^ Cephei::QL::Cashflows::CHaganPricer_Factory::CreateVector (System::Collections::Generic::IEnumerable<IHaganPricer^>^ source)
 {
	 return gcnew CoVector<IHaganPricer^> (source, gcnew CHaganPricerVector ());
 }
/* <summary>Create a single dimension array of HaganPricer with change events</summary> */
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^>^>^ Cephei::QL::Cashflows::CHaganPricer_Factory::CreateCellVector ()
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^> (gcnew CHaganPricerVector ()));
 }
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^>^>^ Cephei::QL::Cashflows::CHaganPricer_Factory::CreateCellVector (System::Collections::Generic::IEnumerable<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^>^ source)
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Cashflows::IHaganPricer^>^> (source, gcnew CHaganPricerVector ()));
 }

